package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * time:2022/11/19 12:41
 *
 * @author:Cc
 */
@CrossOrigin("*")
@RestController
@RequestMapping("admin/storage/")
public class AdminStorageController {

    @Autowired
    private DtsStorageService dtsStorageService;

    /**
     * 上传保存文件
     * @param file
     * @return
     * @throws IOException
     */
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "上传")
    @PostMapping("create")
    public Object add(@RequestPart("file") MultipartFile file) throws Exception {
        // 文件名称
        String realFilename = file.getOriginalFilename();
        // 文件类型, 获取文件的后缀
        String suffix = realFilename.substring(realFilename.lastIndexOf(".") + 1);
        // 文件大小
        long size = file.getSize();
        // 文件转byte数组
        byte[] content = file.getBytes();
        DtsStorage dtsStorage = dtsStorageService.create(suffix, size, realFilename, content);
        if (dtsStorage != null) {
            return ResponseUtil.ok(dtsStorage);
        }
        return ResponseUtil.fail(999, "当前上传文件出错了.检查网络");
    }

    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"对象管理", "查看列表"}, button = "查看列表")
    @GetMapping("list")
    public Object list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort", defaultValue = "add_time") String sort,
                       @RequestParam(value = "order", defaultValue = "desc") String order) {

        IPage<DtsStorage> storageIPage = dtsStorageService.page(page, limit, sort, order);

        // 组装数据
        Map<String, Object> map = new HashMap<>();
        map.put("total", storageIPage.getTotal());
        map.put("items", storageIPage.getRecords());
        return ResponseUtil.ok(map);
    }

    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage DtsStorage) {
        String key = DtsStorage.getKey();
        if (StringUtils.isEmpty(key)) {
            return ResponseUtil.badArgument();
        }
        dtsStorageService.deleteByKey(key);

        return ResponseUtil.ok();
    }



    /**
     * 更新文件
     * @param dtsStorage
     * @return
     */
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage dtsStorage) {
        if (dtsStorageService.update(dtsStorage) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(dtsStorage);
    }
}
